package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * <新闻 - 深圳新闻网> 解析类
 *
 * @author KK
 *
 */
public class SznewsNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex={
				"http://sznews.com/content/[0-9]*-[0-9]*/[0-9]*/content_[0-9]*.htm",
				"http://[a-z]*.sznews.com/content/[0-9]*-[0-9]*/[0-9]*/content_[0-9]*.htm",
				"http://sznews.com/[a-z]*/content/[0-9]*-[0-9]*/[0-9]*/content_[0-9]*.htm",
				"http://[a-z]*.sznews.com/[a-z]*/content/[0-9]*-[0-9]*/[0-9]*/content_[0-9]*.htm",
		};
//
		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		String commnetNum=null;
		String clickNum=null;
		String author=null;
		
		/*
		 * 解析标题
		 */
		title = doc.select("div#PrintTxt h2").text();
		if(StringUtils.isBlank(title))
			title = doc.select("div.newsDetailbod").text();
		if(StringUtils.isBlank(title))
			title = doc.select("h1.con_title").text();
		
		/*
		 * 解析时间
		 */
		date = DateUtils.matchDate(doc.select("span.newtitle_l").text());
		if(date==null)
			date = DateUtils.matchDate(doc.select("div.newsDetailbod").text());
		if(date==null)
			date = DateUtils.matchDate(doc.select("div.con_arc_info").text());
		if(date==null)
			date = DateUtils.matchDate(doc.select("p.sm_q").text());
		if(StringUtils.isBlank(title))
			title = doc.select(".h1-news").text();
		/*
		 * 解析内容
		 */
		content = HtmlCleaner.getContentHtml(url, doc.select("div.new_txt"));
		if(StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("div#PrintTxt"));
		if(StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("div.con_arc_content"));
		
		/*
		 * 解析作者
		 */
		author = JSoupUtils.matchAuthor(doc, "来源：");
		
		NewsMeta news=new NewsMeta();
		news.setUrl(url);
		news.setType(1);
		news.setTitle(StringUtils.trimSpace(title));
		news.setCommentNum(NumberUtils.parseInt(commnetNum));
		news.setClickNum(NumberUtils.parseInt(clickNum));
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);
		news.setAuthor(author);
		return news;
	}



	public static void main(String[] args) {
		SznewsNewsAnalyse a=new SznewsNewsAnalyse();
		String url="http://sznews.com/humor/2015-11/06/content_12440736.htm";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
